//
//  CellOfshoucang.m
//  project-A (pianke)
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "CellOfshoucang.h"

#import "CellTwoTab.h"
#import "CellTwoTab2.h"
#import <UIImageView+WebCache.h>
#import "webOfdic.h"
#import <DKNightVersion.h>
#define WIDTH [UIScreen mainScreen].bounds.size.width


@interface CellOfshoucang ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation CellOfshoucang


-(void)dealloc{
    
    [_tabview release];
    [super dealloc];
    
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.tabview =[[UITableView alloc]init];
        self.tabview.backgroundColor = [UIColor whiteColor];
        
        
        self.tabview.delegate = self;
        self.tabview.dataSource = self;
        
         self.tabview.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        
        self.tabview.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);

        
        [self.contentView addSubview:self.tabview];
        
        self.tabview.separatorStyle =UITableViewCellSeparatorStyleNone;
        
        [self.tabview registerClass:[CellTwoTab class] forCellReuseIdentifier:@"pool1"];
        
        [self.tabview registerClass:[CellTwoTab2 class] forCellReuseIdentifier:@"pool2"];
        
        
         [self.tabview reloadData];
    }
    
    return self;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return WIDTH * 0.225  + 30;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"array.plist"];
    
    NSMutableArray *arr =  [NSMutableArray arrayWithContentsOfFile:file];
    
    webOfdic *dic = [arr objectAtIndex:indexPath.row];
    if ([[dic objectForKey:@"pic_url"] isEqualToString:@""]) {
        CellTwoTab *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        cell.layer.masksToBounds = YES;
        cell.layer.cornerRadius = 10;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cell.layer.borderWidth = 1;
        
        
        cell.title.text = [dic objectForKey:@"title"];
        cell.desc.text = [dic objectForKey:@"desc"];
        
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        NSString *str = [dic objectForKey:@"published"];
        
        NSTimeInterval time=[str doubleValue];
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        
        NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
        
        cell.time.text =  currentDateStr;
        
        
        return cell;
    }else{
        
        CellTwoTab2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        
        cell.title.text = [dic objectForKey:@"title"];
        cell.desc.text = [dic objectForKey:@"desc"];
        
        cell.layer.masksToBounds = YES;
        cell.layer.cornerRadius = 10;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cell.layer.borderWidth = 1;
        
        
        NSString *str = [dic objectForKey:@"published"];
        NSTimeInterval time=[str doubleValue];
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        NSLog(@"date:%@",[detaildate description]);
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        
        NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
        
        
        cell.time.text =  currentDateStr;
        
        [cell.image sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"pic_url"]] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
        
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        return cell;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"array.plist"];
    
    NSMutableArray *arr =  [NSMutableArray arrayWithContentsOfFile:file];
    
    
    return  arr.count;

    
    return 10;
}

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    self.tabview.frame = self.contentView.frame;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%@",indexPath);
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"array.plist"];
    
    NSMutableArray *arr =  [NSMutableArray arrayWithContentsOfFile:file];
    
    webOfdic *dic = [arr objectAtIndex:indexPath.row];
    
    
    [self.delegate didClickWith:dic];
    
}

-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    return layoutAttributes;
    
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
}


@end
